//
//  ShowmeViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/7/19.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "ShowmeViewController.h"
#import "MyOrderViewController.h"
#import "MyAttentionViewController.h"
#import "MyCollectionViewController.h"
#import "MyCompetitionViewController.h"
#import "MydataEditViewController.h"
#import "LoginUserViewController.h"
#import "RegisterNewPhoneCodeViewController.h"
@interface ShowmeViewController ()<UITableViewDelegate,UITableViewDataSource,UIViewControllerTransitioningDelegate>
@property(nonatomic,strong)UITableView *tablview;
@property(nonatomic,strong)UIImageView *headerimageView;
@property(nonatomic,strong)UIView *middleView;
@property(nonatomic, nonnull,strong)NSArray *firstTitileArray;
@property(nonatomic, nonnull,strong)NSArray *secondTitileArray;
@property(nonatomic,assign) BOOL isLogin;
@end

@implementation ShowmeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"秀我";
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
    _isLogin = YES;
    [self createHeaderView];
    [self createTableViewUI];
}
/**
 创建头部视图
 */
-(void)createHeaderView{
    if (_isLogin) {
        _headerimageView = [[UIImageView alloc] init];
        _headerimageView.userInteractionEnabled = YES;
        [self.view addSubview:_headerimageView];
        [_headerimageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self.view);
            make.height.mas_equalTo(scaleTopAndBottom(230));
        }];
        _headerimageView.image = [UIImage imageNamed:@"showmeBg.jpg"];
        UIImageView *iconImageView = [[UIImageView alloc] init];
        [_headerimageView addSubview:iconImageView];
        [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(75, 75));
            make.centerX.equalTo(_headerimageView.mas_centerX);
            make.top.equalTo(_headerimageView.mas_top).offset(scaleTopAndBottom(50));
        }];
        UILabel *nameLabel = [[UILabel alloc] init];
        [_headerimageView addSubview:nameLabel];
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_headerimageView.mas_centerX);
            make.top.equalTo(iconImageView.mas_bottom).offset(scaleTopAndBottom(10));
            make.size.mas_equalTo(CGSizeMake(scaleLeftAndRight(120), scaleTopAndBottom(20)));
        }];
        UILabel *titleLabel = [[UILabel alloc] init];
        [_headerimageView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_headerimageView.mas_centerX);
            make.top.equalTo(nameLabel.mas_bottom).offset(scaleTopAndBottom(5));
            make.size.mas_equalTo(CGSizeMake(WIDTH, scaleTopAndBottom(20)));
        }];
        UILabel *fansCountLabel = [[UILabel alloc] init];
        [_headerimageView addSubview:fansCountLabel];
        [fansCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_headerimageView.mas_centerX);
            make.top.equalTo(titleLabel.mas_bottom).offset(scaleTopAndBottom(5));
            make.size.mas_equalTo(CGSizeMake(WIDTH, scaleTopAndBottom(20)));
        }];
        UIImageView * badgeImageView = [[UIImageView alloc] init];
        [_headerimageView addSubview:badgeImageView];
        [badgeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(25, 25));
            make.bottom.equalTo(nameLabel.mas_top).offset(scaleTopAndBottom(-15));
            make.right.equalTo(nameLabel.mas_right).offset(scaleLeftAndRight(-10));
        }];
        UIImageView *editImageView = [[UIImageView alloc] init];
        editImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(EditPersonalInformation:)];
        [editImageView addGestureRecognizer:tap];
        [_headerimageView addSubview:editImageView];
        [editImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(25, 25));
            make.top.equalTo(_headerimageView.mas_top).offset(scaleTopAndBottom(30));
            make.right.equalTo(_headerimageView.mas_right).offset(scaleLeftAndRight(-10));
        }];
        iconImageView.backgroundColor = [UIColor blueColor];
        nameLabel.backgroundColor = [UIColor lightGrayColor];
        titleLabel.backgroundColor = [UIColor lightGrayColor];
        fansCountLabel.backgroundColor = [UIColor orangeColor];
        badgeImageView.backgroundColor= [UIColor greenColor];
        editImageView.backgroundColor = [UIColor redColor];
    }else{
        [self createNOLogUI];
    }
    _middleView = [[UIView alloc] init];
    [self.view addSubview:_middleView];
    [_middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerimageView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(scaleTopAndBottom(45));
    }];
    UIView *lineVeiw = [[UIView alloc] init];
    [_middleView addSubview:lineVeiw];
    [lineVeiw mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(_middleView);
        make.size.mas_equalTo(CGSizeMake(scaleLeftAndRight(1), scaleTopAndBottom(40)));
    }];
    UIButton *lefeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    lefeBtn.tag = 10001;
    [self.middleView addSubview:lefeBtn];
    [lefeBtn addTarget:self action:@selector(ButtonOnclick:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.tag = 10002;
    [rightBtn addTarget:self action:@selector(ButtonOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.middleView addSubview:rightBtn];
    [lefeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.top.equalTo(_middleView);
        make.right.equalTo(lineVeiw.mas_left).offset(scaleLeftAndRight(-1));
    }];
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.top.equalTo(_middleView);
        make.left.equalTo(lineVeiw.mas_right).offset(scaleLeftAndRight(1));
    }];
    _headerimageView.backgroundColor = [UIColor cyanColor];
    _middleView.backgroundColor = [UIColor whiteColor];
    lefeBtn.backgroundColor = [UIColor redColor];
    rightBtn.backgroundColor = [UIColor redColor];
    lineVeiw.backgroundColor = [UIColor lightGrayColor];
}

/**
 创建未登录时的界面
 */
-(void)createNOLogUI{
    _headerimageView = [[UIImageView alloc] init];
    _headerimageView.userInteractionEnabled = YES;
    [self.view addSubview:_headerimageView];
    [_headerimageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.height.mas_equalTo(scaleTopAndBottom(230));
    }];
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"Hi,等你好久了";
    [_headerimageView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_headerimageView.mas_top).offset(scaleTopAndBottom(110));
        make.left.right.equalTo(_headerimageView);
        make.height.mas_equalTo(scaleTopAndBottom(30));
    }];
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(gotoLogInOrRegisterNewAccount:) forControlEvents:UIControlEventTouchUpInside];
    [_headerimageView addSubview:loginBtn];
    UIButton *regsiterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [regsiterBtn setTitle:@"注册" forState:UIControlStateNormal];
    [regsiterBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [regsiterBtn addTarget:self action:@selector(gotoLogInOrRegisterNewAccount:) forControlEvents:UIControlEventTouchUpInside];
    [_headerimageView addSubview:regsiterBtn];
    loginBtn.tag = 101;
    regsiterBtn.tag = 102;
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_headerimageView.mas_bottom).offset(scaleTopAndBottom(-20));
        make.left.equalTo(_headerimageView.mas_left).offset(scaleLeftAndRight(100));
        make.height.mas_equalTo(scaleTopAndBottom(30));
    }];
    [regsiterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_headerimageView.mas_bottom).offset(scaleTopAndBottom(-20));
        make.right.equalTo(_headerimageView.mas_right).offset(scaleLeftAndRight(-100));
        make.height.mas_equalTo(scaleTopAndBottom(30));
        make.left.equalTo(loginBtn.mas_right).offset(scaleLeftAndRight(20));
        make.width.equalTo(loginBtn.mas_width);
    }];
    loginBtn.backgroundColor = [UIColor purpleColor];
    regsiterBtn.backgroundColor = [UIColor purpleColor];

}

/**
 选择登录还是注册
 */
-(void)gotoLogInOrRegisterNewAccount:(UIButton *)btn{
    if (btn.tag == 101) {
        LoginUserViewController *logInVc = [[LoginUserViewController alloc] init];
        [self presentViewController:logInVc animated:YES completion:nil];
    }
    if (btn.tag == 102) {
        RegisterNewPhoneCodeViewController *registerVc = [[RegisterNewPhoneCodeViewController alloc] init];
        registerVc.headerTitile = @"手机号注册";
        registerVc.isRegister = YES;
        registerVc.type = @"0";
        registerVc.transitioningDelegate = self;
        registerVc.modalPresentationStyle = UIModalPresentationCustom;
        [self presentViewController:registerVc animated:YES completion:nil];
    }
}
/**
   初始化tablview
 */
-(void)createTableViewUI{
    self.tablview = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:self.tablview];
    self.tablview.scrollEnabled = NO;
    [self.tablview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(_middleView.mas_bottom);
        make.bottom.equalTo(self.view.mas_bottom).offset(scaleTopAndBottom(-49));
    }];
    self.tablview.delegate = self;
    self.tablview.dataSource = self;
    UIView *footerView = [[UIView alloc] init];
    self.tablview.tableFooterView = footerView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.firstTitileArray.count;
    }
    return self.secondTitileArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"CellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    if (indexPath.section == 0) {
        cell.imageView.backgroundColor = [UIColor redColor];
        cell.textLabel.text = self.firstTitileArray[indexPath.row];
    }else{
        cell.imageView.image = [UIImage imageNamed:@""];
        cell.textLabel.text = self.secondTitileArray[indexPath.row];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor cyanColor];
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return scaleTopAndBottom(10);
}
/**
 tablview的选中状体

 @return
 */
#pragma mark-tablview的选中状态

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:{
                MyCompetitionViewController *mycompetionVc = [[MyCompetitionViewController alloc] init];
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mycompetionVc];
                nav.transitioningDelegate = self;
                nav.modalPresentationStyle = UIModalPresentationCustom;
                [self presentViewController:nav animated:YES completion:nil];
            }
                break;
            case 1:{
                MyOrderViewController *myorderVc = [[MyOrderViewController alloc] init];
                UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:myorderVc];
                navVc.transitioningDelegate = self;
                navVc.modalPresentationStyle = UIModalPresentationCustom;
                [self presentViewController:navVc animated:YES completion:nil];
            }
                break;
            default:
                break;
        }
    }else{
        switch (indexPath.row) {
            case 0:
            {
                
            }
                break;
            case 1:
            {
                
            }
                break;
            case 2:
            {
                MyCollectionViewController *collecVc = [[MyCollectionViewController alloc] init];
                UINavigationController *collectNav = [[UINavigationController alloc] initWithRootViewController:collecVc];
                collectNav.transitioningDelegate = self;
                collectNav.modalPresentationStyle = UIModalPresentationCustom;
                [self presentViewController:collectNav animated:YES completion:nil];
            }
                break;
            case 3:
            {
                
            }
                break;
            case 4:
            {
                
            }
                break;
            default:
                break;
        }
    }
}

/**
 点击编写个人资料的详细界面


 */
-(void)EditPersonalInformation:(UITapGestureRecognizer *)tap{
    MydataEditViewController *editVc = [[MydataEditViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:editVc];
    nav.transitioningDelegate = self;
    nav.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:nav animated:YES completion:nil];
}
-(void)ButtonOnclick:(UIButton *)btn{
    if (btn.tag == 10001) {
        
    }
    if (btn.tag == 10002) {
//        MyAttentionViewController *attentionVc = [[MyAttentionViewController alloc] init];
//        UINavigationController *attentionNav = [[UINavigationController alloc] initWithRootViewController:attentionVc];
//        [self.navigationController pushViewController:attentionNav animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(NSArray *)firstTitileArray{
    if (_firstTitileArray == nil) {
        _firstTitileArray = @[@"我的赛事",@"我的订单"];
    }
    return _firstTitileArray;
}
-(NSArray *)secondTitileArray{
    if (_secondTitileArray == nil) {
        _secondTitileArray = @[@"申请认证小咖",@"途吉徽章",@"我的收藏",@"常用设置",@"地址管理"];
    }
    return _secondTitileArray;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
